package com.boot.service.impl;

import com.boot.repository.KeyValueRepository;
import com.boot.service.KeyValueService;
import com.boot.vo.KeyValue;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Optional;

public class KeyValueServiceImpl implements KeyValueService {
    @Autowired
    private KeyValueRepository keyValueRepository;

    @Override
    public KeyValue set(KeyValue model) {
        return keyValueRepository.save(model);
    }

    @Override
    public void deleteKey(String key) {
        keyValueRepository.deleteById(key);
    }

    @Override
    public KeyValue getKey(String key) {
        Optional<KeyValue> opt = keyValueRepository.findById(key);
        KeyValue keyValue = opt.get();
        long live = keyValue.getCreateTimes() + keyValue.getLiveTimes();
        if (new Date().getTime() - live > 0) {
            return keyValue;
        }
        return null;
    }
}
